//app.js
var request = require('utils/https.js')
var uri = 'index/user_ajax.html'
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.globalData.userInfo = res.userInfo;
              wx.login({
                success: res => {
                  request.req(uri, 
                  { code: res.code, user_info: this.globalData.userInfo },
                   (err, data) => {
                      wx.setStorageSync('userId', data.data.user_id);
                      wx.setStorageSync('userType', data.data.type);
                      wx.setStorageSync('userMoney', data.data.money);
                  })
                }
              })
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }else{
          wx.navigateTo({
            url: '../../pages/login/login'
          })
        }
      }
    })
  },
  globalData: {
    role: 1, //1:用户角色 2:商家角色 3.业务员角色 4.配送员角色 5。业务员+商户
    userInfo:null,
  }
})